const ProductService = require('../../service/admin/ProductService')

const ProductController = {
    add: async (req, res) => {
        const productData = {
            ...req.body,
            cover: `/productuploads/${req.file.filename}`
        }
        await ProductService.add(productData)
        res.send({
            code: 200,
            msg: '添加成功'
        })
    },
    getList: async (req, res) => {
        const result = await ProductService.getList(req.params.id)
        res.send({
            code: 200,
            msg: '获取成功',
            data: result
        })
    },
    delete: async (req, res) => {
        await ProductService.delete(req.params.id)
        res.send({
            code: 200,
            msg: '删除成功'
        })
    },
    edit: async (req, res) => {
        let productData = {}
        if (req.file) {
            productData = {
                ...req.body,
                cover: `/productuploads/${req.file.filename}`
            }
        } else {
            productData = {
                ...req.body,
            }
        }
        await ProductService.edit(productData)
        res.send({
            code: 200,
            msg: '更新成功'
        })
    }

}

module.exports = ProductController